package edu.ptu.shortlink.admin.toolkit;

import java.security.SecureRandom;

/**
 * @BelongsProject: shortlink
 * @BelongsPackage: edu.ptu.shortlink.admin.toolkit
 * @Author: HYJ
 * @CreateTime: 2025-04-25  17:15
 * @Description: 分组ID随机生成器
 * @Version: 1.0
 */
public final class RandomGenerator {

    private static final String CHARACTERS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
    private static final SecureRandom RANDOM = new SecureRandom();


    /**
     * 生成六位随机分组Id
     *
     * @return 分组Id
     */
    public static String generateRandom() {
        return generateRandom(6);
    }


    /**
     * 生成随机分组id
     *
     * @param len 生成位数
     * @return 分组ID
     */
    public static String generateRandom(int len) {
        StringBuilder sb = new StringBuilder(len);

        for (int i = 0; i < len; i++) {
            int randomIndex = RANDOM.nextInt(CHARACTERS.length());
            sb.append(CHARACTERS.charAt(randomIndex));
        }
        return sb.toString();
    }

}
